* ======================================
* ------- Program: Match 2 step --------
* ======================================

	preserve

	* args:
	* 1: source for voters sample,
	* 2: source for politicians sample
	* 3: network1
	* 4: network2
	* 5: (netmatch/idmatch) matches on network id or unique person id

	
* Politicians
* ===========

	use `2', clear

	if "`5'" == "netmatch"{
		rename Polit_id idPers
		joinby idPers using $dta\Network`3'.dta
		rename idPers `3'`4'Pol_id
		rename `3'net `3'`4'net
	
		* Keep politician with highest rank per year-net-bsu (if several)
		bysort year `3'`4'net Polit_bsu (`3'`4'Pol_id): keep if _n == 1	
	
		rename Polit_knr `3'`4'Pol_knr
		keep `3'`4'net year `3'`4'Pol_knr `3'`4'Pol_id
	}
	
	if "`5'" == "idmatch"{
		rename Polit_id `3'`4'Pol_id
		rename Polit_knr `3'`4'Pol_knr
		keep `3'`4'Pol_id year `3'`4'Pol_knr
	}
	

	tempfile Polit_`3'`4'
	save `Polit_`3'`4'', replace
	
	
* Voters
* ======

	use `1', clear
	
	rename Voter_id idPers
	joinby idPers using $dta\Network`3'`4'.dta
	rename idPers Voter_id
	rename idMediator `3'`4'Med_id
	
	if "`5'" == "netmatch"{
		keep Voter_id year Voter_knr `3'`4'Med_id `3'`4'net
	}
	
	if "`5'" == "idmatch"{
		rename id`3'`4' `3'`4'Pol_id // facilitate match by id
		keep Voter_id year Voter_knr `3'`4'Med_id `3'`4'Pol_id
	}
	
	
* Match
* =====

	if "`5'" == "netmatch"{
		joinby `3'`4'net year using `Polit_`3'`4''
	}
	if "`5'" == "idmatch"{
		joinby `3'`4'Pol_id year using `Polit_`3'`4''
	}
	
	
	* Import knr of mediators
	rename `3'`4'Med_id idPers
	merge m:1 idPers year using $dta/ImportGeo.dta, keep(master matched) ///
		nogen keepusing(knr)
	rename idPers `3'`4'Med_id
	rename knr `3'`4'Med_knr
	
	* Drop politicians outside if voter has connection(s) inside
	gen same = 1*(Voter_knr == `3'`4'Pol_knr & `3'`4'Med_knr == Voter_knr) 	// all 3 in same District 
	bysort Voter_id year: egen same_max = max(same) // 1 if n >= 1 in same D.
	bysort Voter_id year: keep if same == same_max
	drop same same_max
		
		
* Reduce dimensions
* =================

	bysort Voter_id year (`3'`4'Pol_id): keep if _n == 1
	keep Voter_id year `3'`4'Pol_id `3'`4'Pol_knr `3'`4'Med_knr
	
	tempfile match`3'`4'
	save `match`3'`4''
		
	restore
	
	
* Match to default dataset
* ========================

	* Voters and politicians
	merge 1:1 Voter_id year using `match`3'`4'', nogen ///
		keep(master matched)
	
	* Generate estimation variables
	gen `3'`4'Any = 1*(`3'`4'Pol_id != "") 		// Any variable
	gen `3'`4'Same = 1*(`3'`4'Any == 1 & `3'`4'Med_knr == Voter_knr & ///
		Voter_knr == `3'`4'Pol_knr)  			// Same variable

	drop `3'`4'Med_knr `3'`4'Pol_id `3'`4'Pol_knr